import axios from '@/libs/api.request'
import qs from 'qs'

/**
 * @Description: 获取所有学院
 * @Params: {}
 * @Return: { Object } 学院列表
 * @Author: Jackie
 * @Date: 2019/1/25 20:15
 */
export const getInstituteList = () => {
  return axios.request({
    url: '/labm/getInstitute',
    method: 'GET'
  })
}

/**
 * @Description: 获取某个学院的教师列表
 * @Params: { String } institute 学院名
 * @Return: { Object } 教师列表
 * @Author: Jackie
 * @Date: 2019/1/26 18:13
 */
export const getTeacherList = (institute) => {
  return axios.request({
    url: '/info/tea/getTeachersByInstitute',
    method: 'GET',
    params: {
      institute: institute || ''
    }
  })
}

/**
 * @Description: 根据教师的工号获取申请表列表
 * @Params: { Object } filter 检索条件，pageBuilder 分页条件
 * @Return: { Object } 实验室申请表列表
 * @Author: Jackie
 * @Date: 2019/1/29 1:53
 */
export const getAppliedFormList = (filter, pageBuilder) => {
  let appliedFormDTO = {...filter}
  appliedFormDTO.type = appliedFormDTO.type === 'all' ? '' : appliedFormDTO.type
  appliedFormDTO.semester = appliedFormDTO.semester === 'all' ? '' : appliedFormDTO.semester
  appliedFormDTO.teacherCode = appliedFormDTO.teacherCode === 'all' ? '' : appliedFormDTO.teacherCode
  appliedFormDTO.pageBuilder = {...pageBuilder}
  appliedFormDTO.pageBuilder.order = pageBuilder.order.filter(item => {
    return item[Object.keys(item)[0]] !== 'normal'
  })
  return axios.request({
    url: '/labm/app/getByTeaCode',
    method: 'POST',
    data: JSON.stringify(appliedFormDTO)
  })
}

/**
 * @Description: 根据申请表编号获取申请表详情信息
 * @Params: { String } code 申请表编号
 * @Return: { Object } 申请表详情信息
 * @Author: Jackie
 * @Date: 2019/1/28 13:53
 */
export const getAppliedFormDetail = (code) => {
  return axios.request({
    url: '/labm/app/getDetailTable/' + code,
    method: 'GET'
  })
}

/**
 * @Description: 获取课程类别
 * @Params: {}
 * @Return: { Object } 课程类别列表
 * @Author: Jackie
 * @Date: 2019/1/30 14:23
 */
export const getCourseType = () => {
  return axios.request({
    url: '/info/course/getCourseType',
    method: 'GET'
  })
}

/**
 * @Description: 修改申请表状态
 * @Params: { Array, String } codes 需要修改状态的申请表的code，status 状态
 * @Return: { Object } 修改申请表状态结果
 * @Author: Jackie
 * @Date: 2019/2/5 16:31
 */
export const modifyAppFormStatus = (codes, status) => {
  return axios.request({
    url: '/labm/app/updateStatus',
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify({
      codes: codes,
      status: status
    }, {
      arrayFormat: 'brackets'
    })
  })
}

/**
 * @description: 删除申请表中的某一个子实验
 * @param { String } appCode 申请表编号
 * @param { Array } sid 子实验编号
 * @return { Object } 是否删除成功
 */
export const deleteSingleExps = (appCode, sids) => {
  return axios.request({
    url: '/labm/exp/delete',
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify({
      sid: sids
    }, {
      arrayFormat: 'brackets'
    })
  })
}
